import modelAbstract from "./modelAbstract";
import {image} from "../service/images";
import {directionEnum} from "../enum/directionEnum"
// @ts-ignore
import _ from "lodash"
import config from "../../config"
import enemyCan from "../canvas/enemy"
import touchUtil from "../utils/touch"

export default class enemy extends modelAbstract implements IModel{
  public canvas: ICanvas = enemyCan
  name: string = 'big'
  image(): HTMLImageElement {
    return this.randomImage()
  }
  render(): void {
    this.move()
  }
  constructor(position: { x: number, y: number }) {
    super(position)
    setInterval(() => {
      this.randomDirection()
    }, Math.floor(Math.random() * 2000) + 2000)
  }

  // 坦克移动
  public move() {
    while (true) {
      let x = this.position.x
      let y = this.position.y
      switch (this.direction) {
        case directionEnum.bottom:
          y += 2
          break
        case directionEnum.left:
          x -= 2
          break
        case directionEnum.right:
          x += 2
          break
        case directionEnum.top:
          y -= 2
          break
      }
      if (touchUtil.isCanvasTouch(x, y) || touchUtil.isModelTouch(x, y)) {
        this.randomDirection()
      }else {
        this.position.x = x
        this.position.y = y
        break
      }
    }
    super.draw()
  }

  // 随机方向图片
  randomImage() {
    let direction = this.name + _.upperFirst(this.direction)
    return image.get(direction as keyof typeof config.images)!
  }
}
